/*全局状态变量*/
var _flag = 0;
var _pagination = null;
//标识当前是更新还是添加，0标示添加，1标示更新
$(function() {
	$('.doubledate').kuiDate({
		className : 'doubledate',
		isDisabled : "0" // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选
	});

	$.get('../channel/find/list', function(message) {

		$("#channel").empty();

		if (IsEmpty(message)) {
			alert('没有查询到数据');
			return;
		}

		if (message.state === 0) {
			if (message.result.len == 1) {
				var channelName = getHeadIndexValue(0, message, 'channelName');
				var channelId = getHeadIndexValue(0, message, 'channelId');
				$("#channel").append("<option selected='selected' value='" + channelId + "'>" + channelName + "</option>");
				$("#channel").attr('disabled', 'disabled');
			} else {
				$("#channel").append("<option>选择频道</option>");
				for (var i = 0; i < message.result.len; i++) {
					var channelName = getHeadIndexValue(i, message, 'channelName');
					var channelId = getHeadIndexValue(i, message, 'channelId');
					$("#channel").append("<option value='" + channelId + "'>" + channelName + "</option>");
				}
			}
		} else if (message.state == 1) {
			art.dialog({
				fixed : true,
				lock : true,
				content : message.msg,
				icon : 'error',
				ok : true
			});
		} else {
			checkStateShowErrorMsgAndDealResult(JSON.stringify(message));
		}
	}, 'json');

	//初始化 日期选择框
	initImage();
	var startDate = $('#dtBegin').val(getToday());
	var endDate = $('#dtEnd').val(getToday());
});

//初始化图片查看组件
function initImage() {
	Shadowbox.init();
	window.openImage = function(url) {
		Shadowbox.open({
			content : url,
			player : 'img'
		});
	};
}

//返回今天的日期 yyyy-MM-dd 格式
function getToday() {
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth();
	m = parseInt(m) + 1;
	m = m >= 10 ? m : '0' + m;
	var d = date.getDate();
	d = d >= 10 ? d : '0' + d;
	return y + '-' + m + '-' + d;
}

function search() {
	var channelId = $('#channel').val();
	var startDate = $('#dtBegin').val();
	var endDate = $('#dtEnd').val();
	var openName = $('#openName').val();
	var status = $("#searchStatus").val();
	_pagination = new Pagination({
		method : 'get', // 请求方法 post|get
		type : 'json', //返回内容格式，json|text	//NOT SUPPORT
		data : {
			'channelId' : channelId,
			'startDate' : startDate,
			'endDate' : endDate,
			'openName' : openName,
			'status' : status
		},
		url : '../WxCustomerReplayMsg/find/list', // 请求地址，注意函数会自定添加分页的两个参数 ‘0/10’
		pageSize : 10, // 页面大小
		startIndex : 0, //服务器提供的接口页码是从几开始的，建议从0开始
		success : printDataToPage, // state 为  0 时，回调
		pagerHandler : 'pager' // 显示分页的 div 的 id
	});
	//var channelId = $('#channel').val('');
	//var startDate = $('#dtBegin').val('');
	//var endDate = $('#dtEnd').val('');
	//var openName = $('#openName').val('');
}

function printDataToPage(message) {
	$("#tbody").empty();
	var len = message.result.len;
	for (var i = 0; i < message.result.data.length; i++) {
		var id = getHeadIndexValue(i, message, 'id');
		var channelName = getHtmlString(getHeadIndexValue(i, message, 'channelName'));
		var creatTime = getHtmlString(getHeadIndexValue(i, message, 'creatTime'));
		var openName = getHtmlString(getHeadIndexValue(i, message, 'openName'));
		var content = getHtmlString(getHeadIndexValue(i, message, 'content'));
		var imageMId = getHtmlString(getHeadIndexValue(i, message, 'imageMId'));
		var imageUrl = getHtmlString(getHeadIndexValue(i, message, 'imageUrl'));
		var voiceMId = getHtmlString(getHeadIndexValue(i, message, 'voiceMId'));
		var voiceUrl = getHtmlString(getHeadIndexValue(i, message, 'voiceUrl'));
		var location_x = getHtmlString(getHeadIndexValue(i, message, 'location_x'));
		var location_y = getHtmlString(getHeadIndexValue(i, message, 'location_y'));
		var customerName = getHtmlString(getHeadIndexValue(i, message, 'customerName'));
		var mp = getHtmlString(getHeadIndexValue(i, message, 'mp'));
		var tel = getHtmlString(getHeadIndexValue(i, message, 'tel'));
		var levelName = getHtmlString(getHeadIndexValue(i, message, 'levelName'));
		var status = getHtmlString(getHeadIndexValue(i, message, 'status'));
		var musicEvent = '';
		if (IsNotEmpty(voiceUrl)) {
			var musicEvent = "<a id='amr" + i + "' href='#' onclick=amrAudioPlay('" + voiceUrl + "','amr" + i + "')><font style='text-decoration:underline; color: green;'>播放</font></a>";
		}
		var imageTd = '';
		if (IsNotEmpty(imageUrl)) {
			imageTd = "<a href='#' style='text-decoration:underline; color: green;' onclick=openImage('" + imageUrl + "')>查看</a>";
		}

		var check = '';
		if (status == '正常') {
			var check = "<input type='button' class='common_btn' value='推荐' onclick=\"check(" + id + ",1)\">" + "<input type='button' class='common_btn del' value='隐藏' onclick=\"check(" + id + ",2)\">";
		}

		var $tr = $("<tr><td>" + channelName + "</td>" + "<td>" + creatTime + "</td>" + "<td>" + getHtmlString(openName) + "</td>" + "<td>" + content + "</td>" + "<td>" + musicEvent + "</td><td>" + imageTd + "</td>" + "<td>" + getHtmlString(customerName) + "</td>" + "<td>" + mp + "</td>" + "<td>" + status + "</td><td>" + check + "</td></tr>");
		$("#tbody").append($tr);
	}

	$("#sup").text('  共 ' + len + ' 条');
}

function check(id, status) {
	qmsk.get('../WxCustomerReplayMsg/check/' + id + '/' + status, function() {
		_pagination.reload();
		artSuccess("操作成功！");
	});
}

var _id = null;
function openReplyWindow(id) {
	_id = id;
	$("#replyContent").val('');
	$("#replyImage").val('');

	$("#yinying1,#replyWindow").show();
	addMargin($("#replyWindow"));
}

function closeReplyWindow() {
	$("#yinying1,#replyWindow").hide();
}

function reply() {
	$("#yinying1,#replyWindow").hide();
	var content = $("#replyContent").val();
	$.ajaxFileUpload({
		url : "../WxCustomerReplayMsg/replay",
		secureuri : false,
		data : {
			id : _id,
			content : content
		},
		fileElementId : "replyImage",
		dataType : "json",
		type : "post",
		success : function(message, status) {
			if (IsEmpty(message)) {
				art.dialog({
					fixed : true,
					lock : true,
					content : '没有查询到数据',
					icon : 'error',
					ok : true
				});
				return;
			}
			if (message.state === 0) {
				art.dialog({
					fixed : true,
					lock : true,
					content : '回复成功！',
					icon : 'succeed',
					ok : true
				});
			} else if (message.state === 1) {
				art.dialog({
					fixed : true,
					lock : true,
					content : message.msg,
					icon : 'warning',
					ok : true
				});
			} else {
				checkStateShowErrorMsgAndDealResult(JSON.stringify(message));
			}
		}
	});
}

var _autoQueryInterval = null;
/**
 * 开始自动查询
 */
function startAutoQuery() {
	if (_autoQueryInterval !== null) {
		$("#autoQuery").val('自动刷新');
		clearInterval(_autoQueryInterval);
		_autoQueryInterval = null;
		return;
	}
	search();
	$("#autoQuery").val('停止');
	_autoQueryInterval = setInterval(function() {
		search();
	}, 1500);
}
